﻿<%@ Control Language="VB" AutoEventWireup="false" CodeFile="Uc_stp_auth_staff_fv.ascx.vb" Inherits="Apps_Shares_Uc_stp_auth_staff_fv" %>
<%--==============================BEGIN Filter==============================--%>
<table id='tblList' width="50%" border="1" align="center" cellpadding="2" cellspacing="2" bordercolor="#999999">
    <thead id='t1' runat='server'>
        <tr align="center" bgcolor="#99FFFF">
            <td colspan="2" bgcolor="#FFFFFF" class="th_data">
                ค้นหาข้อมูล
            </td>
        </tr>
    </thead>
    <tbody id='t2' runat='server'>
        <tr align="left">
            <td width="47%" bordercolor="#CCCCCC" class="td_box">
                หน่วยงาน :
            </td>
            <td width="53%" bordercolor="#CCCCCC" class="td_box">
                <asp:DropDownList ID='ddlDepartment' runat='server' AutoPostBack="True" DataTextField="department_name"
                    DataSourceID='ddlStpDepartmentSrc' DataValueField="department_code" AppendDataBoundItems="True">
                    <asp:ListItem Value="0" Text="หน่วยงาน"></asp:ListItem>
                </asp:DropDownList>
                <asp:ObjectDataSource ID="ddlStpDepartmentSrc" runat="server" SelectMethod="GetStpDepartmentByPrivilege"
                    TypeName="CodeMatrix.Business.GenericData"></asp:ObjectDataSource>
            </td>
        </tr>
        <tr align="left">
            <td width="47%" bordercolor="#CCCCCC" class="td_box">
                กลุ่มผู้ใช้งาน :
            </td>
            <td width="53%" bordercolor="#CCCCCC" class="td_box">
                <asp:DropDownList ID="ddlGroupUser" runat="server" DataSourceID="ddlGroupUserSrc"
                    DataTextField="group_user_name" DataValueField="group_user_code" AutoPostBack="true">
                    <asp:ListItem Value="0">กลุ่มผู้ใช้งาน</asp:ListItem>
                </asp:DropDownList>
                <asp:SqlDataSource ID="ddlGroupUserSrc" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
                    SelectCommand=" select group_user_code = 0 , group_user_name = 'กลุ่มผู้ใช้งาน'
                                    union all
                                    select group_user_code , group_user_name
                                    from  group_user 
                                    where department_code = @department_code and group_user_status = 1 and group_user_code > 0">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="ddlDepartment" DefaultValue="0" Name="department_code"
                            PropertyName="SelectedValue" />
                    </SelectParameters>
                </asp:SqlDataSource>
            </td>
        </tr>
        <tr align="left">
            <td width="47%" bordercolor="#CCCCCC" class="td_box">
                ผู้ใช้งาน :
            </td>
            <td width="53%" bordercolor="#CCCCCC" class="td_box">
                <asp:DropDownList ID="ddlStaff" runat="server" DataSourceID="ddlStaffSrc" DataTextField="staff_name"
                    DataValueField="staff_code">
                    <asp:ListItem Value="0">ผู้ใช้งาน</asp:ListItem>
                </asp:DropDownList>
                <asp:SqlDataSource ID="ddlStaffSrc" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
                    SelectCommand=" select staff_no = '',staff_code = 0, staff_name = 'ผู้ใช้งาน' 
                                    union all
                                    select
                                    staff_no, 
                                    staff_code , 
                                    cast(staff_no as varchar) + ' ' + staff_firstname + ' ' + staff_lastname as staff_name
                                    from  staff
                                    where @group_user_code = group_user_code and staff_status = 1 and staff_code > 0 order by staff_no">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="ddlGroupUser" DefaultValue="0" Name="group_user_code"
                            PropertyName="SelectedValue" />
                    </SelectParameters>
                </asp:SqlDataSource>
            </td>
        </tr>
    </tbody>
    <tbody id='t3' runat='server'>
        <tr align="center">
            <td colspan="2" bordercolor="#CCCCCC" class="td_data_end">
                <asp:Button ID='btnFilter' runat='server' Text='แสดงข้อมูล'  ValidationGroup='g1' />
                <asp:Button ID='btnMainMenu' Text='<%$ Resources:Resource,BTN_MAINMENU %>' runat='server' PostBackUrl="~/Apps/ViewSystem.aspx?Edit=2" />
            </td>
        </tr>
    </tbody>
    <tbody id='ShowStaff' runat='server'>
        <tr>
            <td colspan="2" align='center' class="text_head2">
                <asp:Literal ID='ltrStaff' runat='server'></asp:Literal>
            </td>
        </tr>
    </tbody>
</table>
<div class="hide"><asp:RequiredFieldValidator ID="ddlDepartmentequiredFieldValidator1" runat="server"
ControlToValidate="ddlDepartment" ErrorMessage="กรุณาเลือกหน่วยงาน" ValidationGroup="g1"
InitialValue="0"></asp:RequiredFieldValidator>
<br />
<asp:RequiredFieldValidator ID="ddlGroupUserRequiredFieldValidator" runat="server"
ControlToValidate="ddlGroupUser" ErrorMessage="กรุณาเลือกกลุ่มผู้ใช้งาน" ValidationGroup="g1"
InitialValue="0"></asp:RequiredFieldValidator>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="ddlStaff" ErrorMessage="กรุณาเลือกผู้ใช้งาน" ValidationGroup="g1"
InitialValue="0"></asp:RequiredFieldValidator>
<br />
<asp:ValidationSummary ID="fvValidationSummary" runat="server" ShowMessageBox='true'
ValidationGroup="g1" /></div>
<%--==============================END Filter==============================--%>
<div style="margin:5px;display:block;width:100%">&nbsp;</div>
<%--==============================BEGIN Gridview==============================--%>
<asp:Literal ID="ltrMenuSystemTable" runat="server"></asp:Literal>
<script src="../Scripts/YAHOO.js" type="text/javascript"></script>
<script src="../Scripts/YUIJSON.js" type="text/javascript"></script>
<script src="../Scripts/utilities.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {
        isCheckAll(<%=rowTotal %>, <%=colTotal %>);
    });

    function saveAuthen() {
        var objMenu = $("input:checkbox");
        var objStaff = [];
        $('#<%=ddlStaff.ClientID%> :selected').each(function(i, selected) {
            objStaff[i] = $(selected).val();
        });

        var cAuthStaff = [];
        var i = 0;
        for (i == 0; i < objStaff.length; i++) {
            var j = 0;
            for (j == 0; j < objMenu.length; j++) {
                if (objStaff[i] == 0) {objStaff[i] = <%= Cint(0+Request.QueryString("Edit"))%>}
                var re = new RegExp(/(col|row)/g);
                if (re.test(objMenu[j].id) == false)
            
                Array.add(cAuthStaff, { auth_staff_code: objMenu[j].authenStaffCode, staff_code: objStaff[i], auth_dep_code: objMenu[j].authenGroupCode, checked: objMenu[j].checked });
            }
        }
        
        if (cAuthStaff.length > 0) {
            var jstring = YAHOO.lang.JSON.stringify(cAuthStaff);
            $.ajax({
                url: "<%=WebServiceURL %>",
                cache: false,
                type: "POST",
                data: ({ jdata: jstring }),
                dataType: "json",
                async: false,
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    var result = eval('(' + XMLHttpRequest.responseXML.text + ')');
                    if (result.Message == 'success') {
                        alert('ข้อมูลถูกบันทึกแล้ว')
                        if (location.href.indexOf('Edit') > -1) {
                            window.location = 'stp_auth_staff_list.aspx?Edit=true';
                        }
                        else {
                            window.location = 'stp_auth_staff_fv.aspx?Insert=1';
                        }
                    } else {
                        alert(result.Message);
                    }
                },
                // Not used because result never return as success
                success: function(response) {
                    var result = eval('(' + response.d + ')');
                    if (result.Message == 'success') {
                        alert('ข้อมูลถูกบันทึกแล้ว')
                        if (location.href.indexOf('Edit') > -1) {
                            window.location = 'stp_auth_staff_list.aspx?Edit=true';
                        }
                        else {
                            window.location = 'stp_auth_staff_fv.aspx?Insert=1';
                        }
                    }
                }
            });
        }
        return false;
    }
</script>
